using Godot;
using Godot.Collections;
using System;
using System.Threading.Tasks;

public partial class UiDialog : Ui
{
	private int index = 0;
	private Label _content;
	private GridContainer _chooseGrid;
	public bool chooseable = false;
	public string textKey = "text";
	public Array<string> text = new Array<string>();
	public string chosen;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		_content = GetNode<Label>("%content");
		_chooseGrid = this.GetNode<GridContainer>("%ChooseGrid");
		var Game = GetNode<Game>("/root/Game");
		MessageManager messageManager = Game.get_message_manager();
		messageManager.DialogStart += RenewTextKey;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.

	public override void _Process(double delta)
	{
		var Game = GetNode<Game>("/root/Game");
		var UIControl = GetNode<UIControl>("/root/UIControl");
		var DialogManager = GetNode<DialogManager>("/root/DialogManager");
		var DIALOG_CHOOSE = ResourceLoader.Load<PackedScene>("res://ui/dialog_choose.tscn");
		
		if(this.Visible == true)
		{
			//string[] dia =DialogManager.dialogtext[index].Split('#');
			var dialog = DialogManager.dialogdic;
			text = dialog[textKey].AsGodotArray<string>();
			if(index == -1)
			{
				index = 0;
				return;
			}
			if(index >= text.Count)
			{return;}
			//GD.Print(textKey.Substring(0, 4));
			if(textKey.Substring(0, 4) == "text")
			{
				if(text[index].Substring(0,1) == "#")
				{
					string[] dia = text[index].Split('#');
					if(dia[1] == "跳转")
					{
						textKey = dia[2];
						index = 0;
					}
				}
				else
				{
					_content.Text = text[index];
					if(Input.IsActionJustReleased("leftmouse"))
					{index++;}
					
				}
			}
			else if(textKey.Substring(0,3) == "cho"||textKey.Substring(0,3) == "get"||textKey.Substring(0,3) == "los")
			{
				GD.Print("弹出选项");
				string[] dia = text[index].Split('#');
				_chooseGrid.Visible = true;
				chooseable = true;
				DialogChoose chooseBlock = (DialogChoose)DIALOG_CHOOSE.Instantiate();
				
				chooseBlock.AddText(dia[1]);
				chooseBlock._button.ButtonChoose += OnChoose;
				_chooseGrid.AddChild(chooseBlock);

				index++;
			}
			else
			{
				if(text[index].Substring(0,1) == "#")
				{
					string[] dia = text[index].Split('#');
					if(dia[1] == "跳转")
					{
						textKey = dia[2];
						index = 0;
					}
				}
				else
				{
					_content.Text = text[index];
					if(Input.IsActionJustReleased("leftmouse"))
					{index++;}
					
				}
			}
			if(index >= text.Count)
			{return;}
			if(text[index] == "#end")
			{
				index = 0;
				textKey = "text";
				this.close_self();
				
			}
			
		}
	}

    public override void enter()
    {
        base.enter();
		//确定对话开始的部分，即textkey
    }

	public void RenewTextKey(Characters character)
	{
		if(character != null)
		{
			textKey = character.CharacterData.textkey;
		}
	}

    public void OnChoose( string buttontext)
	{
		foreach (string _text in text)
		{
			string[] dia = _text.Split('#');
			if(buttontext == dia[1])
			{

				var Game = GetNode<Game>("/root/Game");
				MessageManager messageManager = Game.get_message_manager();
				messageManager.EmitSignal("DialogAnswer",textKey,dia[3]);

				textKey = dia[2];

				index = -1;
				var children = _chooseGrid.GetChildren();
				foreach(var child in children)
				{
					_chooseGrid.RemoveChild(child);
				}
			}
		}
	}
}
